// shopPages/addstaff/addstaff.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    userId: '',
    type: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type
    console.log(type)
    this.setData({
      type: type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.appIdKey)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log(app.globalData.appIdKey)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // },
  inputPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  addMember() {

    var myreg = /^(((13[0-9]{1})|14[0-9]{1}|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.phone.length == 0) {
      wx.showToast({
        title: '输入的手机号为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (this.data.phone.length < 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      var that = this;
  
      wx.request({
        url: app.globalData.bossradar + '/api/1.0/inviteUser',
        data: {
          bossId: app.globalData.userInfo.id,
          phone: that.data.phone,
          belongAppId: app.globalData.appIdKey
        },
        method: 'POST',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          if (res.data.code != 0) {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          } else {
            var uid = res.data.data.uid
            that.setData({
              userId: uid
            })
            wx.showModal({
              title: '提示',
              content: '邀请用户' + res.data.data.userName,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定'),
                    that.confirmInviteUser();
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        },
        complete(e) {
          console.log(e)
          if (e.data.code != 0) {
            wx.showToast({
              title: e.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }
  },

  confirmInviteUser: function () {
    var that = this;
    wx.request({
      url: app.globalData.bossradar + '/api/1.0/confirmInviteUser',
      data: {
        bossId: app.globalData.userInfo.id,
        uid: that.data.userId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.code == 0) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },
  addMember1() {

    var myreg = /^(((13[0-9]{1})|14[0-9]{1}|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.phone.length == 0) {
      wx.showToast({
        title: '输入的手机号为空',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (this.data.phone.length < 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      var that = this;

      wx.request({
        url: app.globalData.bossradar + '/api/1.0/inviteUserBePartner',
        data: {
          bossId: app.globalData.userInfo.id,
          phone: that.data.phone,
          belongAppId: app.globalData.appIdKey
        },
        method: 'POST',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          if (res.data.code != 0) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          } else {
            var uid = res.data.data.uid
            that.setData({
              userId: uid
            })
            wx.showModal({
              title: '提示',
              content: '邀请用户' + res.data.data.userName,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定'),
                    that.confirmInviteUser1();
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        },
        complete(e) {
          console.log(e)
          if (e.data.code != 0) {
            wx.showToast({
              title: e.data.msg,
              icon: 'none'
            })
          }
        }
      })
    }
  },
  confirmInviteUser1: function () {
    var that = this;
    wx.request({
      url: app.globalData.bossradar + '/api/1.0/confirmInviteUserBePartner',
      data: {
        bossId: app.globalData.userInfo.id,
        uid: that.data.userId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.code == 0) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      }
    })
  },
})